package crack.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/30/12
 *         Time: 5:05 PM
 */
public class Problem5 {

    static String f(String str) {
        char[] chars = str.toCharArray();
        StringBuilder result = new StringBuilder();

        char c = chars[0];
        int count = 0;
        for (char aChar : chars) {
            if (c == aChar) {
                count++;
            } else {
                result.append(c).append(count);
                c = aChar;
                count = 1;
            }
        }
        result.append(c).append(count);

        return result.toString();
    }
    
    static String compressBad(String str) {
        String mystr = "";
        char last = str.charAt(0);
        int count = 1;
        for (int i = 1 ; i < str.length() ; i++) {
            if (str.charAt(i) == last) {
                count++;
            } else {
                mystr += last + "" + count;
                last = str.charAt(i);
                count = 1;
            }
        }
        return mystr + last + count;
    }
    
    public static void main(String[] args) {
        String s = "aabcccccaaa";
        String result = f(s);
        System.out.println(result);
        result = compressBad(s);
        System.out.println(result);
    }

}
